import 'package:elevator/widgets/globel_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../lift_state_bg_painter/lift_state_bg_Painter.dart';
import 'spirit_logic.dart';

class SpiritComponent extends StatelessWidget {
  final logic = Get.put(SpiritLogic());
  GestureTapCallback? onTap;
  GestureLongPressCallback? onLongPress;
  Color lLeftColor;
  Color lRightColor;
  Color rLeftColor;
  Color rCenterColor;
  Color rRightColor;
  String lLeftText;
  String lRightText;
  String rLeftText;
  String rCenterText;
  String rRightText;
  ///  0 up, 1 down,2 move,3 bell ,-1 未知
  int curStates;
  //0或1表示未开门 1表示正开门 2表示侧门开
  int liftOpenType = 0;
  ///LiftType=0为普通梯，LiftType=1为扶手梯
  int liftType = 0;
  List<Widget>? widgets;

  double height = 48;
  double frameHeight = 10;

  SpiritComponent({
    this.curStates = -1,
    this.liftType = 0,
    this.liftOpenType = 0,
    this.onTap, this.onLongPress,
    this.lLeftColor = Colors.white, this.lRightColor = Colors.white, this.rLeftColor = Colors.white, this.rCenterColor = Colors.white, this.rRightColor = Colors.white,
    this.lLeftText = '', this.lRightText = '',
    this.rLeftText = '', this.rCenterText = '', this.rRightText = '',
    this.widgets});

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return GestureDetector(
      onTap: onTap,
      onLongPress: onLongPress,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          spaceWidth(width:4),
          Expanded(child: _leftContainer(), flex: 1,),
          spaceWidth(width:4),
          Expanded(child: widgets != null?_rightFlexContainer(widgets!):_rightContainer(), flex: 1,),
          spaceWidth(width:4),
        ],
      ),
    );
  }
  
  _leftComponent({Color backgroundColor = Colors.black, Widget cell = const Text('')}) {
    return Container(
      height: height,
      decoration: BoxDecoration(border: Border(right: BorderSide(color: backgroundColor, width: 0.5))),
      child: Center(child: cell),
    );
  }

  _leftContainer() {
    return Row(
      crossAxisAlignment:CrossAxisAlignment.end,
      children: [
        Expanded(child: Container(
          decoration: BoxDecoration(color: lLeftColor,border: Border.all(color: Colors.black, width: 0.5)),
          child: _leftComponent(cell: Text(lLeftText)),)),
        Expanded(child: Container(
            decoration: BoxDecoration(color: lLeftColor ,border: Border.all(color: Colors.black, width: 0.5)),
            child:_leftComponent(cell: _statusWidget()))),

        Expanded(child: Container(
            height: height + frameHeight,
            child: creatLeftRightView(liftType)))
      ],
    );
  }

  creatLeftRightView(int type){
    if(type==1){
      return Container(
          alignment:Alignment.center,
          margin: EdgeInsets.fromLTRB(0, frameHeight-1, frameHeight-1, 0),
          padding: const EdgeInsets.fromLTRB(4, 0, 4, 0),
          decoration: BoxDecoration(color:Colors.white,border: Border.all(color: Colors.black, width: 0.5)),
          child :const Image(image:AssetImage("images/escalator.png"),));
    }else{
      return Stack(
          alignment:Alignment.bottomLeft,
          fit: StackFit.expand,
          children :[
            CustomPaint(painter:ListStateBgPainter(frameHeight,liftOpenType)),
            Container(
              alignment: Alignment.center,
              margin: EdgeInsets.fromLTRB(0, frameHeight, frameHeight, 0),
              child: Text(lRightText),
            )
          ]);
    }
  }


  _statusWidget() {
    switch(curStates) {
      case 0:
        return const Icon(Icons.arrow_upward, color: Colors.green,);
      case 1:
        return const Icon(Icons.arrow_downward, color: Colors.red,);
      // case 2:
      //   return const Icon(Icons.elevator, color: Colors.green,);
      // case 3:
      //   return const Icon(Icons.doorbell_outlined, color: Colors.orange,);
      default:
        return Container();
    }
  }

  _rightContainer() {
    return SizedBox(
      height: height + frameHeight,
      child: Align(
          alignment:Alignment.bottomLeft ,
          child:Container(
              decoration: BoxDecoration(color:Colors.white,border: Border.all(color: Colors.black, width: 0.5)),
              child:SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Row(
                    children:[
                      Expanded(child: Container(color: rLeftColor,child: _leftComponent(cell: Center(child: Text(rLeftText)),))),
                      Expanded(child: Container(color: rCenterColor, child: _leftComponent(cell: Text(rCenterText)),)),
                      Expanded(child: Container(color: rRightColor, height: 50, child: Center(child: Text(rRightText)),))
                    ]),
              ))),
    );

  }

  _rightFlexContainer(List<Widget> widgets) {
    return SizedBox(
        height: height + frameHeight,
        child: Align(
        alignment:Alignment.bottomLeft ,
        child:Container(
          decoration: BoxDecoration(color:Colors.white,border: Border.all(color: Colors.black, width: 0.5)),
          child:SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Row(
              children: widgets
          ),
      ))),
    );

  }

}
